Skip to content

Conversation

@miaobyte
Copy link
Contributor

getitem

让我帮你分析 __getitem__ 当前支持的索引能力,以便你提交 PR 说明文档:

Tensor.getitem 索引能力支持

1. 基础索引类型

  • 整数索引tensor[0]

    • 返回指定位置的子张量
    • 会自动去除该维度(squeeze)
  • Tensor索引tensor[index_tensor]

    • 支持使用整数类型的tensor进行索引选择
    • 直接调用 indexselect 实现

2. 切片操作

  • 基础切片tensor[:]
    • 支持完整切片 slice(None, None, None)
    • 支持部分切片 slice(start, stop, step)

3. 维度操作

  • None插入新维度tensor[None]tensor[:, None]

    • 在指定位置插入维度1
    • 通过 unsqueeze 实现
  • 省略号tensor[..., 0]

    • 支持使用 ... (Ellipsis)
    • 自动处理中间维度

4. 组合索引

支持上述索引类型的任意组合:

tensor[None, :, None]  # 在首尾增加维度
tensor[:, None, :]     # 在中间增加维度
tensor[..., :3]       # 省略号配合切片

5. 实现细节

  • 将所有索引类型标准化为列表处理
  • 使用 dim_cursor 追踪当前处理的维度
  • 记录需要插入新维度的位置
  • 按顺序处理索引操作

6. 底层算子支持

  • indexselect: 处理张量索引
  • sliceselect: 处理切片操作
  • unsqueeze: 处理维度扩展
  • squeeze: 处理维度压缩

7. 限制

  • 不支持布尔索引
  • 不支持花式索引(advanced indexing)
  • 不支持负步长切片

建议在后续版本中添加这些功能支持。

@miaobyte miaobyte self-assigned this Jun 22, 2025
@miaobyte miaobyte merged commit 5f422bc into array2d:main Jun 22, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant